package org.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.example.reggie.common.R;
import org.example.reggie.config.RedisConfig;
import org.example.reggie.pojo.User;
import org.example.reggie.service.UserService;
import org.example.reggie.utils.SendSms;
import org.example.reggie.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;
//    @PostMapping("/sendMsg")
//    public R<String> sendMsg(@RequestBody User user, HttpSession httpSession){
//        String phone = user.getPhone();
//        if(StringUtils.isNotEmpty(phone)){
//            String code = ValidateCodeUtils.generateValidateCode(4).toString();
////            SendSms sendSms = new SendSms();
////            try {
////                sendSms.sendmessage();
////            } catch (Exception e) {
////                e.printStackTrace();
////            }
//            httpSession.setAttribute(phone,code);
//            return R.success("短信发送成功");
//
//        }
//        return R.error("短信发送失败");
//    }
    @GetMapping("/sendmsg")
    public R<String> sendMsg(HttpSession httpSession){

//        SendSms sendSms = new SendSms();
//        httpSession.setAttribute("19198024374","1234");

        redisTemplate.opsForValue().set("19198024374","1234",5, TimeUnit.MINUTES);
//        try {
//            sendSms.sendmessage();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return R.error("短信发送成功");
    }

    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession httpSession){


        String phone = map.get("phone").toString();
        String code = map.get("code").toString();

//        Object codeinSession = httpSession.getAttribute(phone);
        Object codeinSession = redisTemplate.opsForValue().get(phone);

        if(codeinSession!=null && codeinSession.equals(code)){

            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(lambdaQueryWrapper);

            if(user==null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            httpSession.setAttribute("user",user.getId());

            redisTemplate.delete(phone);
            return R.success(user);
        }
        return R.error("登录失败");
    }

}
